package com.etrans.web.gps.business.log.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.log.domain.PlatformLoginRecordVo;
import com.etrans.web.gps.business.log.service.PlatformLoginRecordService;
import com.etrans.web.gps.business.report.domain.PlatformAnalyzes;
import com.etrans.web.gps.business.system.service.CommonService;

/**
 * 查看平台登陆日志
 * @author         QiLi
 * @ClassName:     PlatformLoginRecordController
 *
 */
@Controller
@RequestMapping("/log/platformLoginRecord/*")
public class PlatformLoginRecordController extends BaseController{

	@Autowired
	private PlatformLoginRecordService platformLoginRecordService;
	@Autowired
	private CommonService commonService;
	
	/**
	 * 平台登录日志 
	 * @Description 页面跳转
	 */
	@RequestMapping("getPlatformLoginRecord.html")
	public String getPlatformLoginRecord(){
		return "views/business/log/platformLoginRecord";
	}
	
	/**
	 *  获取平台登录日志 
	 * @return 
	 */
	@RequestMapping(value = "getPlatformLoginRecordList.html")
	@ResponseBody
	public List<PlatformAnalyzes> getPlatformLoginRecordList(String gnsscenterId, String startTime, String endTime){
		Integer areaId = getSysUser().getArea_id();
		List<PlatformAnalyzes> list = platformLoginRecordService.getPlatformLoginRecordList(areaId, gnsscenterId,startTime,endTime);
        return  list;
	}
	
	/**
	 * 获取平台名称
	 * @return
	 */
	@RequestMapping("queryPlatform.html")
	public @ResponseBody List<HashMap<String,Object>> getQueryEnterAndPlatByRole(){
		int areaId= this.getSysUser().getArea_id();
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("FullID", String.format("%08d",areaId));
		List<HashMap<String,Object>> list = commonService.selectqueryplatformbyAreaID(map);
		return list;
	}
	
	/**
	 *  获取平台登录日志 
	 * @return 
	 */
	@RequestMapping(value = "showLineChart.html")
	@ResponseBody
	public List<PlatformLoginRecordVo> showLineChart(String gnsscenterId, String startTime, String endTime){
		List<PlatformLoginRecordVo> list = platformLoginRecordService.getShowLineChart(gnsscenterId,startTime,endTime);
        return  list;
	}
	
	/**
	 *
	 *@description  导出登录日志数据
	 */
	@RequestMapping(value = "exportPlatformLoginRecord.html")
	@ResponseBody
	public void exportLoginRecord( HttpServletRequest request, HttpServletResponse response, 
			String title, String startTime, String endTime, String gnsscenterId,String colModel) {
		//获取参数
		List<String> colModelList = JSON.parseObject(colModel, List.class);
		List<HashMap<String, Object>> rows = new ArrayList<HashMap<String, Object>>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<PlatformLoginRecordVo> list = platformLoginRecordService.getShowLineChart(gnsscenterId,startTime,endTime);
		for (PlatformLoginRecordVo vo : list) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("id", vo.get_id());
			map.put("isSuccess", vo.getMain_link_state()==0&&vo.getSub_link_state()==0?"连接":"断开");
			map.put("timeStr", sdf.format(vo.getTime()));
			rows.add(map);
		}
		int index=-1;
		List rowsList = new ArrayList<>();
		rowsList.add(rows);
		exportExl(request, response,title, removeHeadName(colModelList,index,-1),rowsList,null);
	}
}
